#!/bin/bash

# 系统状态检查脚本 - Ubuntu 24.04
# 用法: ./check-status.sh

# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

echo "🔍 Web Notes 应用状态检查..."

# 检查系统信息
echo -e "\n=== 系统信息 ==="
echo "系统: $(lsb_release -d | cut -f2)"
echo "内核: $(uname -r)"
echo "运行时间: $(uptime -p)"
echo "负载: $(uptime | awk -F'load average:' '{print $2}')"

# 检查内存和磁盘
echo -e "\n=== 资源使用 ==="
echo "内存使用:"
free -h
echo -e "\n磁盘使用:"
df -h /

# 检查服务状态
echo -e "\n=== 服务状态 ==="
services=("nginx" "mongod")
for service in "${services[@]}"; do
    if systemctl is-active --quiet $service; then
        echo -e "$service: ${GREEN}运行中${NC}"
    else
        echo -e "$service: ${RED}未运行${NC}"
    fi
done

# 检查 PM2 状态
echo -e "\nPM2 应用:"
if command -v pm2 &> /dev/null; then
    pm2 jlist | jq -r '.[] | "\(.name): \(.pm2_env.status)"' 2>/dev/null || pm2 status
else
    echo -e "${RED}PM2 未安装${NC}"
fi

# 检查端口
echo -e "\n=== 端口监听 ==="
ports=("80" "443" "3000" "27017")
for port in "${ports[@]}"; do
    if sudo ss -tulnp | grep -q ":$port "; then
        echo -e "端口 $port: ${GREEN}监听中${NC}"
    else
        echo -e "端口 $port: ${RED}未监听${NC}"
    fi
done

# 检查防火墙
echo -e "\n=== 防火墙状态 ==="
if command -v ufw &> /dev/null; then
    echo "UFW 状态: $(sudo ufw status | head -1)"
    echo "开放端口:"
    sudo ufw status | grep -E "^[0-9]" || echo "无开放端口"
else
    echo "UFW 未安装"
fi

# 检查 SSL 证书
echo -e "\n=== SSL 证书 ==="
if command -v certbot &> /dev/null; then
    cert_count=$(sudo certbot certificates 2>/dev/null | grep "Certificate Name:" | wc -l)
    if [ $cert_count -gt 0 ]; then
        echo -e "SSL 证书: ${GREEN}已安装 ($cert_count 个)${NC}"
        sudo certbot certificates 2>/dev/null | grep -E "(Certificate Name:|Expiry Date:)"
    else
        echo -e "SSL 证书: ${YELLOW}未安装${NC}"
    fi
else
    echo "Certbot 未安装"
fi

# 检查日志
echo -e "\n=== 最近错误日志 ==="
echo "Nginx 错误 (最近5条):"
sudo tail -5 /var/log/nginx/error.log 2>/dev/null || echo "无错误日志"

echo -e "\nMongoDB 错误 (最近5条):"
sudo journalctl -u mongod --no-pager -n 5 --grep="ERROR" || echo "无错误日志"

# 应用健康检查
echo -e "\n=== 应用健康检查 ==="
if curl -s http://localhost:3000/health > /dev/null 2>&1; then
    echo -e "后端 API: ${GREEN}正常${NC}"
else
    echo -e "后端 API: ${RED}异常${NC}"
fi

if curl -s http://localhost/ > /dev/null 2>&1; then
    echo -e "前端页面: ${GREEN}正常${NC}"
else
    echo -e "前端页面: ${RED}异常${NC}"
fi

# 显示访问信息
SERVER_IP=$(ip route get 8.8.8.8 | awk -F"src " 'NR==1{split($2,a," ");print a[1]}')
echo -e "\n=== 访问地址 ==="
echo -e "HTTP: ${YELLOW}http://$SERVER_IP${NC}"

# 检查是否配置了域名
if grep -q "server_name.*;" /etc/nginx/sites-available/web-notes 2>/dev/null; then
    domain=$(grep "server_name" /etc/nginx/sites-available/web-notes | awk '{print $2}' | sed 's/;//' | head -1)
    if [ "$domain" != "_" ]; then
        echo -e "域名: ${YELLOW}http://$domain${NC}"
        if sudo certbot certificates 2>/dev/null | grep -q "$domain"; then
            echo -e "HTTPS: ${GREEN}https://$domain${NC}"
        fi
    fi
fi

echo -e "\n${GREEN}状态检查完成！${NC}"